; (function ($) {
    var methods = {
        init: function () {
            if (document.cookie.indexOf('RTX_KEY') > -1) {

            } else if (!navigator.userAgent.match(/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i)) {
                return;
            }

            if (!window.LangMsg) {
                window.LangMsg = {
                    msg_wmp_more: "更多"
                };
            }

            $(".action").hide();

            $("button[buttype='btnPrint']").remove();

            var buttons = $(".action button");
            var button_length = 0;

            $(".action button").each(function () {
                if ($(this).css("display") != "none") {
                    button_length += 1;
                }
            });

            if (button_length == 0) {
                if (Mobile) {
                    $("div.bottom").prev().css("bottom", 0);
                    $("div.bottom").remove();
                }
                return;
            }

            var nowButtons = [];

            buttons.each(function (index, item) {
                if ($(this).css("display") == "none") {
                    return;
                }

                var item = {
                    sort: 0,
                    name: $(item).find("span").text(),
                    type: $(item).attr("buttype"),
                    icon: $(item).find("i").attr("class")
                };

                nowButtons.push(item);
            });

            if (nowButtons.length == 0) {
                $("div.bottom").prev().css("bottom", 0);
                $("div.bottom").remove();
                return;
            }

            var button_sort = [
                { sort: 1, type: "btnStart" },
                { sort: 2, type: "btnApproval" },
                { sort: 3, type: "btnSendback" },
                { sort: 4, type: "btnAbandon" },
                { sort: 5, type: "btnSave" },
                { sort: 6, type: "btnCopySend" },
                { sort: 7, type: "btnTranfer" },
                { sort: 8, type: "btnHastenWork" },
                { sort: 9, type: "btnRepeal" },
                { sort: 10, type: "btnStartQuery" },
                { sort: 11, type: "btnAnswerQuery" },
                { sort: 12, type: "btnCancelQuery" },
                { sort: 13, type: "btnPrint" }
            ];

            var button_html = '<nav class="navbar navbar-default navbar-fixed-bottom" role="navigation" style="z-index:9999;position: fixed;border-top: 1px solid #d8d8d8;">';
            button_html += '<div class="nav-container-fix">';
            var sub_list = "";

            var sort_index = 0;
            $(button_sort).each(function (index, item) {
                $(nowButtons).each(function (index, now_item) {
                    if (item.type == now_item.type) {
                        item.name = now_item.name;
                        if (sort_index > 2) {
                            sub_list += methods.render_sub(item.name, item.type, now_item.icon, (button_length - 3), (sort_index - 3 + 1));
                        } else {

                            if ((sort_index == 2 && button_length > 3)) {
                                button_html += methods.render(window.LangMsg['msg_wmp_more'], "", now_item.icon, button_length, (sort_index + 1));
                                sub_list += methods.render_sub(item.name, item.type, now_item.icon, (button_length - 2), (sort_index - 3 + 1));
                            } else {
                                button_html += methods.render(item.name, item.type, now_item.icon, button_length, (sort_index + 1));
                            }
                        }
                        sort_index++;
                    }
                });
            });
            $(nowButtons).each(function (index, now_item) {
                var hasButn = false;
                $(button_sort).each(function (index, item) {
                    if (item.type == now_item.type) {
                        hasButn = true;
                        return false;
                    }
                });

                if (hasButn) {
                    return;
                } else {
                    if (sort_index > 2) {
                        sub_list += methods.render_sub(now_item.name, now_item.type, now_item.icon, (button_length - 3), (sort_index - 3 + 1));
                    } else {
                        if ((sort_index == 2 && button_length > 3)) {
                            button_html += methods.render(window.LangMsg['msg_wmp_more'], "", now_item.icon, button_length, (sort_index + 1));
                            sub_list += methods.render_sub(now_item.name, now_item.type, now_item.icon, (button_length - 2), (sort_index - 3 + 1));
                        } else {
                            button_html += methods.render(now_item.name, now_item.type, now_item.icon, button_length, (sort_index + 1));
                        }
                    }
                    sort_index++;
                }
            });

            $("#moveAction").append(button_html);

            if (sub_list != "") {
                var sub_html = '<div class="new-sub">';
                sub_html += '<ul>';
                sub_html += sub_list;
                sub_html += '</ul>';
                sub_html += '<div class="tiggle"></div>';
                sub_html += '<div class="innertiggle"></div>';
                sub_html += '</div>';
                $(".menu-last").append(sub_html);
            }

            $(".menu").click(function () {
                if ($(this).hasClass("cura")) {
                    $(this).children(".new-sub").hide(); //当前菜单下的二级菜单隐藏
                    $(".menu").removeClass("cura"); //同一级的菜单项
                } else {
                    $(".menu").removeClass("cura"); //移除所有的样式
                    $(this).addClass("cura"); //给当前菜单添加特定样式
                    $(".menu").children(".new-sub").hide(); //隐藏所有的二级菜单
                    $(this).children(".new-sub").show(); //展示当前的二级菜单
                }

                if ($(this).children(".new-sub").length == 0) {
                    var button_type = $(this).attr("button_type_name");

                    $(".action button[buttype='" + button_type + "']").click();
                }
            });

            $(".new-sub ul li").click(function () {
                var button_type = $(this).attr("button_type_name");
                $(".action button[buttype='" + button_type + "']").click();
            });
        },
        render: function (button_name, button_type, btn_icon, total, now) {
            var menu_fix_class = "", menu_icon = "", button_html = "";
            menu_icon = btn_icon;
            if (total == 1) {
                menu_fix_class = "menu-all";
            } else if (total == 2) {
                menu_fix_class = "menu-avg";
            } else if (total > 3 && now == 3) {
                menu_fix_class = "menu-last";
                menu_icon = "fa fa-bars blue-color";
            }

            button_html = '<div class="menu ' + menu_fix_class + '" button_type_name="' + button_type + '">';
            button_html += '<i class="' + menu_icon + '"></i><span class="btn-name">' + button_name + '</span>';
            button_html += '</div>';

            return button_html;
        },
        render_sub: function (sub_button_name, sub_button_type, btn_icon, total, now) {
            var sub_fix = "";
            if (now > 0) {
                sub_fix = "border-top";
            }
            return '<li class="' + sub_fix + '" button_type_name="' + sub_button_type + '"><i class="' + btn_icon + '"></i><span  class="btn-name">' + sub_button_name + '</span></li>';
        }
    };
    $.fn.WmpButton = function (method) {
        if (methods[method]) {
            return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
        } else if (typeof method === 'object' || !method) {
            return methods.init.apply(this, arguments);
        } else {
            $.error('Method' + method + 'does not exist');
        }
    };
})($);